#include "sessions.h"

int Sessions :: Create( )
{
	PGresult * res = PQexec( conn, "CREATE TABLE sessions ( iduser integer NOT NULL, idsession integer NOT NULL UNIQUE, lasttime integer, CONSTRAINT sessions_pkey PRIMARY KEY ( iduser ) );" );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Sessions :: Clean( )
{
	PGresult * res = PQexec( conn, "DROP TABLE sessions;" );
	//			cout << PQresultStatus( res ) << endl;
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Sessions :: FindRecord( int iduser, int & idsession ) // +
{
	char idus[ 32 ];
	sprintf( idus, "%i", iduser );

	char * st1;
	ExtraClass :: GetString( st1, "SELECT idsession FROM sessions WHERE iduser = ", idus, " LIMIT 1;" );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	//			cout << "\t\t" << result << endl;
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;
			idsession = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
		}
		else
		{
			result = DB_NO_RECORD;
			idsession = -1;
		}
	}
	else
	{
		result = DB_NO_RESULT;
		idsession = -1;
	}
	//			cout << "idsession " << idsession << "\t" << result << endl;
	PQclear( res ); // ???
	return result;
}

int Sessions :: GetIduser( int idsession, int & iduser) // по idsession узнаем iduser +
{
	char idses[ 32 ];
	sprintf( idses, "%i", idsession );

	char * st1;
	ExtraClass :: GetString( st1, "SELECT iduser FROM sessions WHERE idsession = ", idses, " LIMIT 1;" );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;
			iduser = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
		}
		else
		{
			result = DB_NO_RECORD;
			iduser = -1;
		}
	}
	else
	{
		result = DB_NO_RESULT;
		iduser = -1;
	}
	PQclear( res );
	return result;
}

int Sessions :: NewIdsession( int & idsession ) // +
{
	int result = DB_OK + 1;
	//			srand ( time( NULL ) );

	while ( result != DB_OK )
	{
		idsession = rand( );
		//				cout << "NewIdsession( ) idsession = " << idsession << endl;

		char idses[ 32 ];

		sprintf( idses, "%i", idsession );

		char * st1;
		ExtraClass :: GetString( st1, "SELECT idsession FROM sessions WHERE idsession = ", idses, " LIMIT 1;" );

		PGresult * res = PQexec( conn, st1 );
		if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
		{
			if ( PQntuples( res ) )
			{
				char * endptr;
				idsession = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
			}
			else
				result = DB_OK;
		}
		else
			result = DB_NO_RESULT;
		PQclear( res );
		//				cout << "NewIdsession( ) idsession = " << idsession << endl;
	}

	return result;
}

int Sessions :: InsRecord( int iduser, int & idsession ) // +
{
	int result;

	//			cout << "InsRecord( ) idsession = " << idsession << endl;

	NewIdsession( idsession );

	char idus[ 32 ];
	char idses[ 32 ];
	char time_[ 32 ];

	sprintf( idus, "%i", iduser );
	sprintf( idses, "%i", idsession );
	sprintf( time_, "%i", time ( NULL ) );

	char * st1;
	ExtraClass :: GetString( st1, "INSERT INTO sessions (iduser,idsession,lasttime) VALUES (", idus,",",idses,",",time_, ");" );

	PGresult * res = PQexec( conn, st1 );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = FindRecord( iduser, idsession );
	PQclear( res );

	//			cout << "InsRecord( ) idsession = " << idsession << endl;

	return result;
}

int Sessions :: DelOldRecord( ) // +
{
	int result;

	char time_[ 32 ];

	sprintf( time_, "%i", time ( NULL ) - 15 * 60 );

	char * st1;
	ExtraClass :: GetString( st1, "DELETE FROM sessions WHERE lasttime <= ",time_, ";" );

	PGresult * res = PQexec( conn, st1 );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );

	//			cout << "InsRecord( ) idsession = " << idsession << endl;

	return result;
}
